<?php

class Modunit {

	function __construct() {}
	
	function getAllModules() {
		global $db;
		$sql = "SELECT * FROM modules ORDER BY ModuleName ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
		
	function getAllUnits() {
		global $db;
		$sql = "SELECT * FROM unit ORDER BY UnitTitle ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function getAllUnitsInModule($mid) {
		global $db;
		$sql = "SELECT * FROM unit WHERE ModuleID='".$mid."' ORDER BY UnitTitle ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function getAllTestsInModuleUnit($mid, $unitid) {
		global $db;
		$sql = "SELECT * FROM tests WHERE ModuleID='".$mid."' AND UnitID='".$unitid."' AND Status!=0 ORDER BY Title ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function getAllTestsInModule($mid) {
		global $db;
		$sql = "SELECT * FROM tests WHERE ModuleID='".$mid."' AND UnitID IS NULL AND Status!=0 ORDER BY Title ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function getAllTestsInUnit($unitid) {
		global $db;
		$sql = "SELECT * FROM tests WHERE UnitID='".$unitid."' AND Status!=0 ORDER BY Title ASC";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function getMIDfromUID($uid) {
		global $db;
		$sql = "SELECT ModuleID FROM unit WHERE UnitID='".$uid."'";
		$result = $db->db_query($sql);
		if($result) {
			$row = $db->db_fetchrow($result);
			return $row[0];
		} else return false;
	}
	
	function getTestDetail($tid) {
		global $db;
		$sql = "SELECT * FROM tests WHERE TestID='".$tid."'";
		$result = $db->db_query($sql);
		if($result) {
			$row = $db->db_fetchrow($result);
			return $row;
		} else return false;
	}
	
	function insert_Test($data) {
		global $db;
		if($data['UnitID'] == "") {
			$sql = "INSERT INTO tests(
				`TestID`,
				`ModuleID`,
				`Title`,
				`Description`,
				`Deadline`,
				`TimeLimit`,
				`Status`,
				`UnitID`
			) VALUES(
				NULL,
				'".$data['ModuleID']."',
				'".$data['Title']."',
				'".$data['Description']."',
				'".$data['Deadline']."',
				'".$data['TimeLimit']."',
				'".$data['Status']."',
				NULL
			)";
		} else {
			$sql = "INSERT INTO tests(
				`TestID`,
				`ModuleID`,
				`Title`,
				`Description`,
				`Deadline`,
				`TimeLimit`,
				`Status`,
				`UnitID`
			) VALUES(
				NULL,
				'".$data['ModuleID']."',
				'".$data['Title']."',
				'".$data['Description']."',
				'".$data['Deadline']."',
				'".$data['TimeLimit']."',
				'".$data['Status']."',
				'".$data['UnitID']."'
			)";
		}
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}
	
	function update_Test($data) {
		global $db;
		$sql = "UPDATE tests SET Title='".$data['Title']."', Description='".$data['Description']."', DeadLine='".$data['Deadline']."', 
				TimeLimit='".$data['TimeLimit']."' WHERE TestID='".$data['TestID']."'";
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}
	
	function delete_Test($tid) {
		global $db;
		$sql = "UPDATE tests SET Status=0 WHERE TestID=$tid";
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}
	
	function insertQuestionToTest($qbid, $tid) {
		global $db;
		$sql = "INSERT INTO testquestions(TestID, QuestionNo, Question, MaxMarks, ModelAnswer, Option1, Option2, Option3, Option4, 
				Option5, SystemAnswerOption1, SystemAnswerOption2, SystemAnswerOption3, SystemAnswerOption4, SystemAnswerOption5) 
				SELECT $tid, qb.QuestionBankID, qb.Question, qb.Mark, qb.ModeAnswer, qb.Option1, qb.Option2, qb.Option3, qb.Option4, 
				qb.Option5, qb.SystemAnswerOption1, qb.SystemAnswerOption2, qb.SystemAnswerOption3, qb.SystemAnswerOption4, qb.SystemAnswerOption5 
				FROM questionbank qb 
				WHERE qb.QuestionBankID=$qbid";
		//cho $sql;
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}
	
	function getAllQuestionInTest($tid) {
		global $db;
		$sql = "SELECT * FROM testquestions WHERE TestID=$tid";
		$result = $db->db_query($sql);
		if($result) {
			$rows = $db->db_fetchrowset($result);
			return $rows;
		} else return false;
	}
	
	function deleteQuestionInTest($tid, $qbid) {
		global $db;
		$sql = "DELETE FROM testquestions WHERE TestID=$tid AND QuestionNo=$qbid";
		$result = $db->db_query($sql);
		if($result) {
			return true;
		} else return false;
	}
}

?>